package pers.cz.quartz.job;

import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.quartz.JobListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 执行监听器
 * @program: PostGirl-panent
 * @description: QuartzJobListener
 * @author: Cheng Zhi
 * @create: 2021-10-14 14:46
 **/
public class QuartzJobListener implements JobListener {

    Logger log = LoggerFactory.getLogger(QuartzJobListener.class);
    ListenerThead thread = null;
    String jobId = "0";
    Long timeOut = 0L;
    String jobName = "";
    public QuartzJobListener(Long jobId, Long timeOut, String jobName) {
      this.jobId = String.valueOf(jobId);
      this.timeOut = timeOut;
      this.jobName = jobName;
    }

    /**
     * 监听器的名字
     * @return
     */
    @Override
    public String getName() {
        return this.jobId;
    }

    /**
     * 任务开始触发
     * @param jobExecutionContext
     */
    @Override
    public void jobToBeExecuted(JobExecutionContext jobExecutionContext) {
        this.thread = new ListenerThead(Long.valueOf(jobId), timeOut, jobName);
        thread.start();
    }

    /**
     * “否决JobDetail”是在Triiger被其相应的监听器监听时才具备的能力
     * @param jobExecutionContext
     */
    @Override
    public void jobExecutionVetoed(JobExecutionContext jobExecutionContext) {

    }

    /**
     * 执行结束触发
     * @param jobExecutionContext
     * @param e
     */
    @Override
    public void jobWasExecuted(JobExecutionContext jobExecutionContext, JobExecutionException e) {

        System.out.println("监听器触发结束");
        thread.stopThread();
    }
}
